#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <fstream>
#include <boost/lexical_cast.hpp>

#include <mlgesture.h>

using namespace std;
using namespace mlgesture;

MainWindow::MainWindow(QWidget *parent) :
	QDialog(parent),
	ui(new Ui::MainWindow)
{
	ui->setupUi(this);
	loadSettings();
	connect(ui->skinButton, SIGNAL(clicked()), this, SLOT(showSkinDialog()));
	skinDialog = NULL;
}

MainWindow::~MainWindow()
{
	delete ui;
	if (skinDialog) {
		delete skinDialog;
	}
}

void MainWindow::accept() {
	if (saveSettings()) {
		if (skinDialog) {
			delete skinDialog;
			skinDialog = NULL;
		}
		QDialog::accept();
	}
}

void MainWindow::reject() {
	if (skinDialog) {
		delete skinDialog;
		skinDialog = NULL;
	}
	QDialog::reject();
}

void MainWindow::loadSettings() {
	int camId_ = -1;
	int lowHue_ = 179;
	int upperHue_ = 45;
	int lowValue_ = 70;
	int upperValue_ = 255;
	int trackingSensity_ = 92;
	fstream f("demo.cfg");
	string line;
	if (f.is_open()) {
		getline(f, line);
		while (f.good()){
			size_t pos = line.find_first_of(":");
			if (pos == string::npos) {
				break;
			}
			string cmd = line.substr(0, pos);
			string value = line.substr(pos+1);
			int ivalue;
			try {
				ivalue = boost::lexical_cast<int>(value);
			}
			catch (boost::bad_lexical_cast&) {
				break;
			}
			if (cmd.compare("camera_id")==0) {
				camId_ = ivalue;
			}
			else if (cmd.compare("low_hue")==0) {
				if (ivalue < 0 || ivalue > 359) {
					break;
				}
				lowHue_ = ivalue;
			}
			else if (cmd.compare("upper_hue")==0) {
				if (ivalue < 0 || ivalue > 359) {
					break;
				}
				upperHue_ = ivalue;
			}
			else if (cmd.compare("low_value")==0) {
				if (ivalue < 0 || ivalue > 255) {
					break;
				}
				lowValue_ = ivalue;
			}
			else if (cmd.compare("upper_value")==0) {
				if (ivalue < 0 || ivalue > 255) {
					break;
				}
				upperValue_ = ivalue;
			}
			else if (cmd.compare("tracking_sensity")==0) {
				if (ivalue < 0 || ivalue > 100) {
					break;
				}
				trackingSensity_ = ivalue;
			}
			getline(f, line);
		}
	}
	ui->camIdEdit->setText(QString::number(camId_));
	ui->lowSkin->setText(QString::number(lowHue_));
	ui->upperSkin->setText(QString::number(upperHue_));
	ui->lowValue->setText(QString::number(lowValue_));
	ui->upperValue->setText(QString::number(upperValue_));
	ui->sensitySpin->setValue(trackingSensity_);

	f.close();
}

bool MainWindow::saveSettings() {
	fstream f("demo.cfg", ios_base::out | ios_base::trunc);
	if (!f.is_open()) {
		return false;
	}
	f << "camera_id:" << ui->camIdEdit->text().toStdString() << endl;
	f << "low_hue:" << ui->lowSkin->text().toStdString() << endl;
	f << "upper_hue:" << ui->upperSkin->text().toStdString() << endl;
	f << "low_value:" << ui->lowValue->text().toStdString() << endl;
	f << "upper_value:" << ui->upperValue->text().toStdString() << endl;
	f << "tracking_sensity:" << ui->sensitySpin->value() << endl;

	f.close();

	return true;
}

void MainWindow::showSkinDialog() {
	if (!skinDialog) {
		skinDialog = new SkinThresholdDialog(ui->camIdEdit->text().toInt(), ui->lowSkin->text().toInt(), ui->upperSkin->text().toInt(),
											 ui->lowValue->text().toInt(), ui->upperValue->text().toInt(), this);
		connect(skinDialog, SIGNAL(accepted()), this, SLOT(saveSkin()));
	}
	skinDialog->show();
}

void MainWindow::saveSkin() {
	if (skinDialog) {
		ui->lowSkin->setText(QString::number(skinDialog->getLowThreshold()));
		ui->upperSkin->setText(QString::number(skinDialog->getUpperThreshold()));
		ui->lowValue->setText(QString::number(skinDialog->getValueLowThreshold()));
		ui->upperValue->setText(QString::number(skinDialog->getValueUpperThreshold()));
	}
}
